package xs.szw.service.controller.role;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import xs.szw.common.model.Option;
import xs.szw.common.result.Result;
import xs.szw.service.model.vo.RoleVo;
import xs.szw.service.model.dto.RoleDto;
import xs.szw.service.model.pojo.sys.Role;
import xs.szw.service.service.RoleService;

import java.util.List;

import static xs.szw.common.result.Result.judge;
import static xs.szw.common.result.Result.success;

/**
 * @program: xs_oa
 * @description:
 * @author: Songzw
 * @create: 2025-02-07 09:43
 **/
@Tag(name = "角色管理")
@RestController
@RequestMapping("/sys/roles")
//@Validated
@RequiredArgsConstructor
public class RoleController {


    private final RoleService roleService;

    @PostMapping("")
    @Operation(summary = "创建角色")
    public Result<Object> createRole(@Valid @RequestBody  RoleDto roleDto){
        roleService.createRole(roleDto);
        return success(null,"角色创建成功！");
    }

    @PutMapping("")
    @Operation(summary = "修改角色")
    public Result<Object> updateRole(@RequestBody RoleDto roleDto){
        boolean b = roleService.updateRole(roleDto);
        return judge(b);
    }

    @DeleteMapping("")
    @Operation(summary = "删除角色")
    @Parameter(name = "id", description = "角色Id", required = true, example = "1024")
    public Result<Object> deleteRoleWithRelations(@RequestParam Long id){
        boolean b = roleService.deleteRoleWithRelations(id);
        return judge(b);
    }

    @DeleteMapping("/batch")
    @Operation(summary = "批量删除角色")
    public Result<Object> deleteBatchRoleWithRelations(@RequestBody List<Long> ids){
        boolean b = roleService.deleteBatchRoleWithRelations(ids);
        return judge(b);
    }

    @GetMapping("")
    @Operation(summary = "查询角色")
    @PreAuthorize("hasAnyAuthority('123')")
    @Parameter(name = "id", description = "角色Id", required = true, example = "1024")
    public Result<Object> getRole(@RequestParam Long id){
        RoleVo roleVo = roleService.getRole(id);
        return success(roleVo,"角色查询成功！");
    }

    @GetMapping("/list")
    @Operation(summary = "查询角色列表")
    public Result<Object> getRoleList(){
//        List<Role> roleList = roleService.getRoleList();
        return success(null,"查询角色列表！");
    }

    @GetMapping("/page")
    @Operation(summary = "查询角色分页列表")
    @Parameters(
            {
                    @Parameter(name = "current",description = "当前页"),
                    @Parameter(name = "size",description = "每页显示数量"),
                    @Parameter(name = "roleName",description = "角色名"),
                    @Parameter(name = "token",required = true,description = "jwt令牌",in = ParameterIn.HEADER)
            }
    )
    public Result<Object> getRolePage(@RequestParam Integer current,@RequestParam Integer size,@RequestParam(required = false) String roleName){
        Page<RoleVo> rolePage = roleService.getRolePage(current, size, roleName);
        return success(rolePage,"查询角色分页列表");
    }


    @GetMapping("/options")
    @Operation(summary = "查询角色下拉列表")
    public Result<Object> getRoleOptions(){
        List<Option<Long>> roleList = roleService.getRoleList();
        return success(roleList,"查询角色下拉列表");
    }


}
